<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item label="用户账号" prop="username">
      <el-input v-model="ruleForm.username" />
    </el-form-item>
    <el-form-item label="用户密码" prop="password">
      <el-input
        v-model="ruleForm.password"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        注册账号
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
  <el-button type="primary" size="default" @click="handleLiveBatch">
    转存生活馆数据
  </el-button>
</template>

<script setup>
import { reactive, ref } from "vue";
import { userReg } from "../../api/user";
import { ElMessage } from "element-plus";
import axios from "axios";
import request from "../../utils/request";
const ruleFormRef = ref();

const checkAge = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("请输入账号")); //表单验证失败
  }
  callback(); //通过验证
};

const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("Please input the password"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass");
    }
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("Please input the password again"));
  } else if (value !== ruleForm.password) {
    callback(new Error("Two inputs don't match!"));
  } else {
    callback();
  }
};
// 表单数据
const ruleForm = reactive({
  password: "",
  checkPass: "",
  username: "",
});
//表单验证逻辑
const rules = reactive({
  password: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
  username: [{ validator: checkAge, trigger: "blur" }],
});

// 表单提交事件
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      //验证通过
      console.log("submit!", ruleForm);
      let { username, password } = ruleForm;
      let msg = await userReg(username, password); //触发注册请求
      ElMessage(msg); //提示注册结果
    } else {
      console.log("error submit!");
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

const handleLiveBatch = async () => {
  let wzjUrl = `/hehe/proxy/articles/featured/list?limit=1000&offset=0&latitude=&longitude=`;
  let res = await axios.get(wzjUrl);
  console.log(res.data.results);
  let list = res.data.results.map((item) => {
    return {
      method: "POST", // 3.1 说明本条数据的操作意图（新增、删除？）
      path: "/1.1/classes/Live", //3.2 指定本条数据操作的目标，Post是动态表名
      body: item,
    };
  });
  await request.post("/batch", { requests: list });
};
</script>
